package com.example.blades.xmlex0411;


import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class xmlEx1 extends AppCompatActivity {
    RadioButton r1=null;
    RadioButton r2=null;
    RadioButton r3=null;
    RadioButton r4=null;
    RadioGroup  radioGroup=null;
    RadioButton currentRadioButton=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml_ex1);

        //获得单选按钮 用 findViewById
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        r1 = (RadioButton) findViewById(R.id.a);
        r2 = (RadioButton) findViewById(R.id.b);
        r3 = (RadioButton) findViewById(R.id.c);
        r4 = (RadioButton) findViewById(R.id.d);
        r1.setClickable(true);

        //监听单选按钮
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            //获取按钮组所选定的按钮
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                currentRadioButton= (RadioButton) findViewById(group.getCheckedRadioButtonId());
            }
        });

        Button btn1_sure=(Button)findViewById(R.id.sure);
        Button btn2_cancel=(Button)findViewById(R.id.cancel);

        btn1_sure.setOnClickListener(new btn1_sure());
        btn2_cancel.setOnClickListener(new btn2_cancel());
    }

    class btn1_sure implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String ans="";
            if (currentRadioButton == null) {
                setTitle("你没有选择！");
            }
//            } else if (currentRadioButton.getText().equals("on")) {
//                setTitle("你选择的答案是：正确的！");
//            } else {
//                setTitle("你选择的答案是：错误的！");
//            }

            if (r1.isChecked())
            {
                ans="in";
            }
            else if(r2.isChecked())
            {
                ans="on";
            }
            else if(r3.isChecked())
            {
                ans="of";
            }
            else if(r4.isChecked())
            {
                ans="to";
            }

            /*new一个Intent对象，并指定class*/
            Intent intent=new Intent();
            //设置Intent对象要启动的Activity
            intent.setClass(xmlEx1.this,otherActivity.class);
            /*new 一个Bundle对象，并将要传递的数据传入*/
            Bundle bundle=new Bundle();
            bundle.putString("ans",ans);
            /*将Bundle对象assign给Intent*/
            intent.putExtras(bundle);

            // 通过Intent对象启动另外一个Activity
            xmlEx1.this.startActivity(intent);
        }
    }


    class btn2_cancel implements View.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            radioGroup.clearCheck();
            setTitle("");
        }
    }

}


